package com.penghaisoft.microwms.material.web.intowarehouse.controller;

import java.util.HashMap;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.common.wms.model.RawCarCheckIn;
import com.penghaisoft.microwms.material.web.intowarehouse.service.IRawCarCheckInService;

/**
 * @Description 原材料条码入库
 * @author luoteng
 * @date 2018年5月23日 下午5:26:31
 * @version 1.0
 */
@RestController
@RequestMapping(value = "intowarehouse/carcheckin")
@Slf4j
public class RawCarCheckInController extends BaseController {

	@Autowired
	private IRawCarCheckInService rawCarCheckInService;
	
	@Autowired
	private IWmsCommonService wmsCommonService;

	/** 
	 * @Title list
	 * @Description 查询列表
	 * @author luoteng
	 * @date 2018年7月9日:下午5:49:43
	 * @param page
	 * @param rows
	 * @param rawCarCheckIn
	 * @return TransResult
	 */
	@PostMapping("list")
	public TransResult list(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			RawCarCheckIn rawCarCheckIn) {
		Pager<RawCarCheckIn> result = rawCarCheckInService.findListByCondition(
				page, rows, rawCarCheckIn);
		return success(result);
	}
	
	/** 
	 * @Title proHandInCreate
	 * @Description 车辆灯登记新增
	 * @author luoteng
	 * @date 2018年7月10日:上午9:41:05
	 * @param list
	 * @return TransResult
	 */
	@PostMapping("/batch/insert")
	public TransResult proHandInCreate(@RequestBody List<RawCarCheckIn> list) {
		TransResult transResult = new TransResult();
		if(list == null || list.isEmpty()){
			transResult.setCode(Constant.FAIL);
			transResult.setMsg("请选择原材料型号及数量！");
			return transResult;
		}else{
			String checkInNo = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_DJ, Constant.SEQ_PREFIX_SHORT_REDIS_RAW_DJ, 1).get(0);
			Resp resp = rawCarCheckInService.batchInsert(list, checkInNo, getLoginName());
			
			transResult.setCode(resp.getCode());
			transResult.setMsg(resp.getMsg());
		}
		
		return transResult;
	}
	
	/** 
	 * @Title proHandInCreate
	 * @Description 车辆灯登记删除
	 * @author luoteng
	 * @date 2018年7月10日:上午9:41:05
	 * @param list
	 * @return TransResult
	 */
	@PostMapping("/del")
	public TransResult proHandInDel(RawCarCheckIn rawCarCheckIn) {
		TransResult transResult = new TransResult();
		rawCarCheckIn.setActiveFlag("1");
		
		List<RawCarCheckIn> list = rawCarCheckInService.queryByAny(rawCarCheckIn);
		if(list == null){
			transResult.setCode(Constant.FAIL);
			transResult.setMsg("请选择记录！");
			return transResult;
		}else{
			RawCarCheckIn tmp = list.get(0);
//			是否生成条码： 1：是 0：否
			if(tmp.getUserDefined2() != null && tmp.getUserDefined2().equals("1")){
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该车辆登记单号已经生成条码，不能删除！");
				return transResult;
			}
			
			rawCarCheckIn.setActiveFlag("0");
			Resp resp = rawCarCheckInService.update(rawCarCheckIn);
			
			transResult.setCode(resp.getCode());
			transResult.setMsg(resp.getMsg());
		}
		
		return transResult;
	}
	
	/** 
	 * @Title createBarcode
	 * @Description 根据登记单号生成条码
	 * @author luoteng
	 * @date 2018年7月10日:上午11:11:05
	 * @param rawCarCheckIn
	 * @return TransResult
	 */
	@PostMapping("barcode/create")
	public TransResult createBarcode(RawCarCheckIn rawCarCheckIn) {
		TransResult transResult = new TransResult();
		if(rawCarCheckIn.getCheckInNo() == null){
			transResult.setCode(Constant.FAIL);
			transResult.setMsg("请选择登记单号！");
			return transResult;
		}else{
			rawCarCheckIn.setActiveFlag("1");
			
			List<RawCarCheckIn> list = rawCarCheckInService.queryByAny(rawCarCheckIn);
			if(list == null){
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("请选择登记单号！");
				return transResult;
			}else{
				RawCarCheckIn tmp = list.get(0);
//				是否生成条码： 1：是 0：否
				if(tmp.getUserDefined2() != null && tmp.getUserDefined2().equals("1")){
					transResult.setCode(Constant.FAIL);
					transResult.setMsg("该车辆登记单号已经生成条码！");
					return transResult;
				}
			}
			
			HashMap<String, List<String>> barcodeMap = new HashMap<String, List<String>>();
			for(RawCarCheckIn tmp : list){
				List<String> barcodeList = wmsCommonService.getProBarCodeShortSeq5(tmp.getModelType(), tmp.getModelType(), tmp.getAmount());
//				barcodeMap.put(tmp.getModelType(), barcodeList);
				barcodeMap.put(tmp.getModel(), barcodeList);
			}
			
			
			
			Resp resp = rawCarCheckInService.createBarcode(list, barcodeMap, getLoginName());
			
			transResult.setCode(resp.getCode());
			transResult.setMsg(resp.getMsg());
		}
		
		return transResult;
	}
}
